package user;

import operation.*;

import java.util.*;

public class AdminUser extends User{

    Scanner scan = new Scanner(System.in);
    public AdminUser(String name) {
        super(name);
        this.ioPerations = new IOPeration[] {
                new Leave(),
                new ShowBook(),
                new FindBook(),
                new AddBook(),
                new DeleteBook(),
                new ReviseBook()
        };
    }
    static int temp = 0;//表示未登录(管理员)
    //管理员的总和用法
    public int DoUser() {
        if (temp == 0) {
            ArrayList<Integer> list = new ArrayList<>();
            //初始密码
            list.add(123456789);
            while (true) {
                System.out.println("请输入:");
                System.out.println("1.登录   2.创建密码");
                int N = scan.nextInt();
                if (N == 1) {//登录
                    if (Log(list)) {//密码和验证码正确
                        break;
                    } else {
                        System.out.println("输入错误");
                    }
                } else if (N == 2) {//修改密码
                    System.out.println("请输入新密码(9位):");
                    list.add(scan.nextInt());
                }
            }
        }
            return menu();
    }
    //登录部分方法
    public boolean Log(ArrayList<Integer> list) {
        System.out.println("请输入密码:");
        int log = scan.nextInt();
        if (list.indexOf(log) >= 0) {
            if(random()) {
                System.out.println("登录成功!");
                temp++;
                return true;
            }
        }
        return false;
    }
    //验证码部分方法
    public boolean random(){
        System.out.println("请输入验证码:");
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        int num1 = random.nextInt(5);//唯一数字出现的位置
        for (int i = 0; i < 5; i++) {
            int num2 = random.nextInt(2);//字母的大小写
            if(i == num1){
                int n = random.nextInt(10)+48;//数字的Ascii
                sb.append((char)n);
            }else if(num2 == 0){
                int n = random.nextInt(26)+97;//小写字母的Ascii
                sb.append((char)n);
            }else if(num2 == 1){
                int n = random.nextInt(26)+65;//小写字母的Ascii
                sb.append((char)n);
            }
        }
        String s = sb.toString();
        System.out.println("验证码:" + s);
        String s0 = scan.next();
        return s0.equals(s);
    }
    //菜单部分方法
    public int menu(){
        System.out.println("欢迎 " + name + " 来到梦幻图书店");
        System.out.println("------管理员菜单------");
        System.out.println("1.展示图书   2.查找图书");
        System.out.println("3.上架图书   4.下架图书");
        System.out.println("5.修改图书信息        ");
        System.out.println("      0.离开书店      ");
        System.out.println("--------------------");
        System.out.println("请输入操作:");
        int n = scan.nextInt();
        return n;
    }
}
